package com.tour.manage;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.tour.model.Member;

public class MemberManager {
	
	private HibernateTemplate ht = null;
	private SessionFactory sessionFactory = null;
	
	private HibernateTemplate getHibernateTemplate() {
		if(ht == null) {
			ht = new HibernateTemplate(sessionFactory);
		}
		
		return ht;
	}
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public Integer addMember(Member member) {
		return (Integer) getHibernateTemplate().save(member);
	}

	public void deleteMember(Member member) {
		getHibernateTemplate().delete(member);
	}

	public void deleteMemberByID(Integer id) {
		getHibernateTemplate().delete(get(id));
	}

	public Member get(Integer id) {
		return (Member) getHibernateTemplate().get(Member.class, id);
	}

	public void modifyMember(Member member) {
		getHibernateTemplate().update(member);
	}

	@SuppressWarnings("unchecked")
	public List<Member> findByLoginName(String loginName) {
		return (List<Member>) getHibernateTemplate().find(
				"from Member m where m.loginName=?", loginName);
	}
}
